<template>
  <div class="rankCard">
    <div class="rank-header">来访省份排名</div>
    <div class="rank-label">
      <span>排名</span>
      <span>访问量</span>
    </div>
    <div class="rank-body">
      <div class="rank-item" v-for="item in rankData" :key="item.rank">
        <div class="item-left">
          <div class="num" :style="{ background: item.color }">{{ item.rank }}</div>
          <div class="name">{{ item.name }}</div>
        </div>
        <div class="item-right">{{ item.value }}</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, defineProps, watch, onMounted } from 'vue'

const props = defineProps({
  data: {
    type: Array,
    default: null
  }
})

const rankColor = ref({
  1: '#FF9700',
  2: '#2CBF77',
  3: '#4D99F2',
  4: '#73B2FB',
  5: '#AAD1FF'
})
watch(
  () => props.data,
  () => {
    addColor()
  },
  { deep: true }
)
const rankData = ref()
const addColor = () => {
  if (!props.data) return
  rankData.value = props.data.map(item => {
    return {
      ...item,
      color: rankColor.value[item.rank] || '#d3d3dd'
    }
  })
}
onMounted(() => {
  addColor()
})
</script>

<style lang="scss" scoped>
.rankCard {
  margin-top: 37px;
  .rank-header {
    margin-bottom: 14px;
    font-size: 14px;
    color: #333333;
  }
  .rank-label {
    display: flex;
    justify-content: space-between;
    font-size: 12px;
    color: #aaaaaa;
  }
  .rank-body {
    .rank-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 13px 0;
      border-bottom: 1px solid #e4e4e4;
      .item-left {
        display: flex;
        align-items: center;
        line-height: 20px;
        .num {
          margin-right: 10px;
          width: 20px;
          height: 20px;
          border-radius: 4px;
          font-size: 12px;
          text-align: center;
          color: #ffffff;
        }
        .name {
          font-size: 14px;
          color: #333333;
        }
      }
      .item-right {
        line-height: 20px;
        font-size: 12px;
        color: #444444;
      }
    }
  }
}
</style>
